require("../util.js");
require("../component/components.js");
require("../component/seeHeader.js");
require("../component/basicSlide.js");
require("../component/selfmediaslide.js");
require("../component/selfmediaflow.js");
require("../component/footer.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
// if(!userInfo){
//     location.href="/loginIndex"
// }
// console.log(userInfo);
var vm = new Vue({
    el:"#seeIndex",
    data:{
        id:userInfo.id,
        isNav:1,
        loadFlag:false,
        recommendMain:{},//轮播 top10
        indexSlide1:{},//顶部轮播
        recommendList:{},//推荐广告
        recommendList1:[],
        recommendList2:[],
        hotAdvList:{
            list:{}
        },//人气广告
        pageNumber:1,
        userCategoryList:{}//分类信息
    },
    mounted:function(){
        //sessionStorage.clear()
        //this.init();
        this.getRecommendMain();
        this.getMainHotAdvList();
        // this.getHotAdvList();
        // this.getRecommendList();
        // this.getUserCategoryList();
        lazyload();
    },
    methods:{
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        //初始化
        init:function(){
           // this.$refs.seeHeader.getUserCategoryList(1,this.id);
        },
        //乐看推荐获取轮播图 top10
        getRecommendMain:function(){
            var that=this;
            this.ajax({url:"/getRecommendMain",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.recommendMain=b.json.data;
                }else{
                    vm.$refs.prompt.toast("获取轮播失败！")
                }
            });
        },
        //获取主页推荐广告列表
        getMainHotAdvList:function(){
            var that=this;
            var data={
                pageNumber:that.pageNumber
            };
            this.ajax({url:"/selfMedia/getMainHotAdvList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.recommendList=b.json.data;
                    that.recommendList1=that.recommendList.list;
                    if(that.recommendList.pageNumber>1){
                        if(that.recommendList.list.length>0) {
                            that.recommendList2 = that.recommendList2.concat(b.json.data.list);
                        }
                    }else{
                        that.recommendList2= that.recommendList1;
                    }
                    vm.loadFlag=false;
                    lazyload();
                }else{
                    that.recommendList={};
                    that.recommendList1=[];
                    that.recommendList2=[];
                    vm.$refs.prompt.toast("获取推荐广告失败！")
                }
            });
        },
        //跳转
        getNext:function(num,item){
            console.log(item)
            if(num==1){
                if(item.type==1){   // 1自媒体 2企业广告3城市广告4传统文化
                    location.href="/see/selfMediaVd?id="+item.id+"&uid="+userInfo.id+"&sourceType=1";
                }else if(item.type==2){
                    location.href="/see/adDetails?id="+item.id+"&uid="+item.uid;
                }else if(item.type==3){
                    location.href="/cityAd/cityAdVideo?cityAdvId="+item.id;
                }else if(item.type==4){

                }
            }else if(num==2){

            }

        }
    }
    ,watch: {
        recommendList2: function () {
            if(this.recommendList2.length!=0){
                this.$nextTick(function(){
                    fmasonry();
                    $("img.lazy").lazyload({
                        failurelimit:10,
                        load:fmasonry
                    });
                    console.log($(".selfMedia-tabCent").height());
                    // if(getWindowHeight())

                });
            }
            // this.$nextTick(function () {
            //     console.log($(".selfMedia-tabCent").height());
            //     fmasonry();
            //     $("img.lazy").lazyload({
            //         failurelimit: 10,
            //         load: fmasonry
            //     });
            // });
        }
    }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag = vm.recommendList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            vm.getMainHotAdvList();
            lazyload();
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function fmasonry() {
    minigrid('.selfMedia-tabCent', '.selfMedia-list',9);
    // $(".selfMedia-tabCent").masonry({
    //     gutterWidth: 10,
    //     itemSelector: '.selfMedia-list',
    //     isAnimated: true,
    //     isFitWidth:true
    // });
}
function lazyload(){
    $("img.lazy").lazyload();
}